﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Collections;

namespace Ko2.Web.Utils
{
    internal sealed class XmlAttributeHelper
    { 
        public static string GetString(XmlNode node, string attributeName)
        {
            return GetString(node, attributeName, null);
        }
 
        public static string GetString(XmlNode node, string attributeName, string defaultValue)
        {
            XmlAttribute attribute = node.Attributes[attributeName];
            if (attribute != null)
            {
                return attribute.InnerText;
            }
            return defaultValue;
        }
 
        public static bool GetBoolean(XmlNode node, string attributeName)
        {
            return GetBoolean(node, attributeName, false);
        }
 
        public static bool GetBoolean(XmlNode node, string attributeName, bool defaultValue)
        {
            string value = GetString(node, attributeName);
            if (value != null)
            {
                IList trueItems = new string[] { "true", "1", "yes" };
                return trueItems.Contains(value.ToLower());
            }
            return defaultValue;
        }
 
        public static int GetInt32(XmlNode node, string attributeName)
        {
            return GetInt32(node, attributeName, 0);
        }
 
        public static int GetInt32(XmlNode node, string attributeName, int defaultValue)
        {
            try
            {
                string value = GetString(node, attributeName);
                if (value != null)
                {
                    return int.Parse(value);
                }
            }
            catch { }
            return defaultValue;
        }
 
        public static double GetDouble(XmlNode node, string attributeName)
        {
            return GetDouble(node, attributeName, 0);
        }
 
        public static double GetDouble(XmlNode node, string attributeName, double defaultValue)
        {
            try
            {
                string value = GetString(node, attributeName);
                if (value != null)
                {
                    return double.Parse(value);
                }
            }
            catch { }
            return defaultValue;
        }
 
        public static object GetEnum(XmlNode node, string attributeName, Type enumType, object defaultValue)
        {
            try
            {
                string value = GetString(node, attributeName);
                if (value != null)
                {
                    return Enum.Parse(enumType, value, true);
                }
            }
            catch { }
            return defaultValue;
        }
 
        public static Guid GetGuid(XmlNode node, string attributeName)
        {
            return GetGuid(node, attributeName, Guid.Empty);
        }
 
        public static Guid GetGuid(XmlNode node, string attributeName, Guid defaultValue)
        {
            try
            {
                string value = GetString(node, attributeName);
                if (value != null)
                {
                    return new Guid(value);
                }
            }
            catch { }
            return defaultValue;
        }
 
        public static Type GetType(XmlNode node, string attributeName)
        {
            return GetType(node, attributeName, null);
        }
 
        public static Type GetType(XmlNode node, string attributeName, Type defaultValue)
        {
            try
            {
                string value = GetString(node, attributeName);
                if (value != null)
                {
                    return Type.GetType(value);
                }
            }
            catch { }
            return defaultValue;
        }
 
        public static void SetValue(XmlNode node, string name, object value)
        {
            SetValue(node, name, value != null ? value.ToString() : null);
        }
 
        public static void SetValue(XmlNode node, string name, string value)
        {
            if (node != null)
            {
                XmlAttribute attribute = node.Attributes[name, node.NamespaceURI];
                if (attribute == null)
                {
                    attribute = node.OwnerDocument.CreateAttribute(name,node.OwnerDocument.NamespaceURI);
                    node.Attributes.Append(attribute);
                }
                attribute.InnerText = value;
            }
        }
 
        public static void SetValue(XmlNode node, string name, int value)
        {
            SetValue(node, name, value.ToString());
        }
 
        public static void SetValue(XmlNode node, string name, DateTime value)
        {
            SetValue(node, name, value.ToString());
        }
 
        public static void SetValue(XmlNode node, string name, bool value)
        {
            SetValue(node, name, value ? "1" : "0");
        }
 
        public static void SetValue(XmlNode node, string name, Guid value)
        {
            SetValue(node, name, (value.Equals(Guid.Empty) ? null :value.ToString()));
        }
 
 
        private static object ParseValue(Type type, string value)
        {
            object parsedValue = null;
 
            if (type.Equals(typeof(string)))
            {
                // string
                parsedValue = value;
            }
            else if (type.Equals(typeof(short)))
            {
                // short
                parsedValue = short.Parse(value);
            }
            else if (type.Equals(typeof(int)))
            {
                // int
                parsedValue = int.Parse(value);
            }
            else if (type.Equals(typeof(long)))
            {
                // long
                parsedValue = long.Parse(value);
            }
            else if (type.Equals(typeof(float)))
            {
                // float
                parsedValue = float.Parse(value);
            }
            else if (type.Equals(typeof(double)))
            {
                // double
                parsedValue = double.Parse(value);
            }
            else if (type.Equals(typeof(bool)))
            {
                // bool
                IList trueValues = (IList)new string[] { "1", "-1", "true", "yes","ja" };
                parsedValue = trueValues.Contains(value.ToLower());
            }
            else if (type.Equals(typeof(decimal)))
            {
                // decimal
                parsedValue = decimal.Parse(value);
            }
            else if (type.Equals(typeof(DateTime)))
            {
                // DateTime
                parsedValue = DateTime.Parse(value);
            }
            else if (type.IsEnum)
            {
                // Enumerations
                parsedValue = Enum.Parse(type, value, true);
            }
            return (parsedValue);
        }
    }
}
